﻿using System;
using System.Configuration;

namespace Qiao.Common.Core
{
    /// <summary>
    /// 配置文件操作类
    /// </summary>
    public sealed class ConfigHelper
    {
        #region 得到AppSettings中的配置字符串信息
        /// <summary>
        /// 得到AppSettings中的配置字符串信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetAppSettingsString(string key)
        {
            string CacheKey = "AppSettings-" + key;
            object objModel = DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = ConfigurationManager.AppSettings[key];
                    if (objModel != null)
                    {
                        DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
                    }
                    else
                    {
                        RecordError("GetConfigString", "配置文件" + key + "未找到");
                        objModel = "";
                    }
                }
                catch
                {
                    RecordError("GetConfigString", "配置文件" + key + "未找到");
                }
            }
            return objModel.ToString();
        } 
        #endregion

        #region 得到AppSettings中的配置Bool信息
        /// <summary>
        /// 得到AppSettings中的配置Bool信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool GetAppSettingsBool(string key)
        {
            bool result = false;
            string cfgVal = GetAppSettingsString(key);
            if (null != cfgVal && string.Empty != cfgVal)
            {
                try
                {
                    result = bool.Parse(cfgVal);
                }
                catch (FormatException)
                {
                    RecordError("GetConfigBool", "配置文件" + key + "未找到");
                }
            }
            else
            {
                RecordError("GetConfigBool", "配置文件" + key + "未找到");
            }

            return result;
        } 
        #endregion

        #region 得到AppSettings中的配置Decimal信息
        /// <summary>
        /// 得到AppSettings中的配置Decimal信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static decimal GetAppSettingsDecimal(string key)
        {
            decimal result = 0;
            string cfgVal = GetAppSettingsString(key);
            if (null != cfgVal && string.Empty != cfgVal)
            {
                try
                {
                    result = decimal.Parse(cfgVal);
                }
                catch (FormatException)
                {
                    RecordError("GetConfigDecimal", "配置文件" + key + "未找到");
                }
            }
            else
            {
                RecordError("GetConfigDecimal", "配置文件" + key + "未找到");
            }

            return result;
        } 
        #endregion

        #region 得到AppSettings中的配置int信息
        /// <summary>
        /// 得到AppSettings中的配置int信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int GetAppSettingsInt(string key)
        {
            int result = 0;
            string cfgVal = GetAppSettingsString(key);
            if (null != cfgVal && string.Empty != cfgVal)
            {
                try
                {
                    result = int.Parse(cfgVal);
                }
                catch (FormatException)
                {
                    RecordError("GetConfigInt", "配置文件" + key + "未找到");
                }
            }
            else
            {
                RecordError("GetConfigInt", "配置文件" + key + "未找到");
            }
            return result;
        }
        #endregion

        #region 得到connectionStrings中的配置信息
        /// <summary>
        /// 得到connectionStrings中的配置信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConnectionStringsString(string key)
        {
            string CacheKey = "Conn-" + key;
            object objModel = DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = ConfigurationManager.ConnectionStrings[key];
                    if (objModel != null)
                    {
                        DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
                    }
                    else
                    {
                        RecordError("GetConfigString", "配置文件" + key + "未找到");
                        objModel = "";
                    }
                }
                catch
                {
                    RecordError("GetConfigString", "配置文件" + key + "未找到");
                }
            }
            return objModel.ToString();
        }
        #endregion

        private static void RecordError(string methodName, string errorMessage)
        {
            ErrorHelper.RecordError("ConfigHelper", methodName, errorMessage);
        }
    }
}
